<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/lib/layui/css/layui.css">
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <header style="height: 100%">
            <div align="left">
                <table style="width: 100%">
                    <tr>
                        <td>
                            <form class="form-inline" onsubmit="return false">
                                <div class="form-group">
                                    模块名：
                                    <input id="name" type="text" class="form-control" placeholder="模块名">
                                    <button id="searchBt" class="layui-btn layui-btn-sm" permission="sys:module:query"><i class="layui-icon">&#xe615;</i>搜索</button>
                                </div>
                            </form>
                        </td>
                        <td align="right">
                            <button class="layui-btn layui-btn-sm" onclick="location.href='addModule.html'" permission="sys:module:add">
                                <i class="layui-icon">&#xe608;</i> 添加
                            </button>
                        </td>
                    </tr>
                </table>
            </div>
        </header>

        <div>
            <div class="widget-body no-padding">
                <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                    <tr>
                    </tr>
                    <tr>
                        <th>模块名称</th>
                        <th>访问路径</th>
                        <th>是否展示</th>
                        <th>排列序号</th>
                        <th>模块描述</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/js/jq.js"></script>
<script type="text/javascript" src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="/js/my/permission.js"></script>
<script type="text/javascript" src="/lib/layui/layui.js"></script>
<script type="text/javascript" src="/js/dict.js"></script>
<script type="text/javascript">
    layui.use([ 'layer' ], function() {
        var layer = layui.layer;
    });
    var userStatus = showDictSelect("status", "userStatus", true);

    var pers = checkPermission();

    var example;
    function init(){
        example =
            $('#dt-table').DataTable({
                "searching": false,
                "processing": false,
                "serverSide" : true,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url" : "/sys/module/list",
                    "type":"get",
                    "data":function(d){
                        d.name = $('#name').val();
                    },
                    "error":function(xhr, textStatus, errorThrown){
                        var msg = xhr.responseText;
                        console.log(msg);
                        var response = JSON.parse(msg);
                        var code = response.code;
                        var message = response.message;
                        if (code == 400) {
                            layer.msg(message);
                        } else if (code == 401) {
                            localStorage.removeItem("token");
                            layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function(){
                                location.href = '/login.html';
                            });
                        } else if (code == 403) {
                            console.log("未授权:" + message);
                            layer.msg('未授权');
                        } else if (code == 500) {
                            layer.msg('系统错误：' + message);
                        }
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                "columns": [
                    { "data": "name", "defaultContent": ""},
                    { "data": "indexPage", "defaultContent": ""},
                    { "data": "ifShow", "defaultContent": ""},
                    { "data": "sort", "defaultContent": ""},
                    { "data": "description", "defaultContent": ""},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable":false,
                        "render": function (data, type, row) {
                            var id = row['id'];
                            var href = "updateModule.html?id=" + id;
                            var edit = buttonEdit(href, "sys:module:add", pers);
                            var del = buttonDel(id, "sys:module:del", pers);
                            return edit+del;
                        }
                    },

                ],
                "order": [[3, "asc"]]
            } );
    }

    $("#searchBt").click(function(){
        example.ajax.reload();
    });
    function del(id){
        layer.confirm('确定要删除吗？', {
            btn : [ '确定', '取消' ]
        }, function() {
            $.ajax({
                type : 'delete',
                url : '/sys/module/' + id,
                success : function(data) {
                    location.reload();
                }
            });
        });
    }
    init();
</script>